<template>
    <div class="AddPermission-container content-wrapper">
        <el-row>
            <el-col>
                <span class="margin-right-5">角色：{{this.$route.query.roleName}}</span>
            </el-col>
        </el-row>
        <el-row class="margin-bottom-10">
            <el-col :span="24">
                <el-button type="primary" size="medium" @click="handleCheckedAll">全选</el-button>
                <el-button type="danger" size="medium" @click="handleRemoveCheckedAll">清空</el-button>
                <el-button type="primary" size="medium" @click="handleResetChecked">重置</el-button>
                <el-button type="primary" size="medium" @click="back()">返回</el-button>
                <el-button type="primary" size="medium" @click="handleSavePermission">分配权限到角色</el-button>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="8">
                <el-tree
                        :data="data"
                        :props="defaultProps"
                        :default-expand-all="true"
                        :check-strictly="false"
                        node-key="id"
                        v-loading="loading"
                        highlight-current
                        show-checkbox
                        @node-click="handleNodeClick"
                        ref="tree"
                ></el-tree>
            </el-col>
            <el-col :span="16"></el-col>
        </el-row>
    </div>
</template>

<script>
    import { selectTree } from '@/api/upms/permission'
    import { addPermissionToRole, getRolePermissionAssignList } from '@/api/upms/role'
    export default {
        name: 'AddPermission',
        data() {
            return {
                loading: false,
                data: [],
                idKeys: [],
                defaultProps: {
                    label: 'name',
                    children: 'children'
                },
            }
        },
        created() {
            this.getTreeData()
        },
        methods: {
            back() {
                this.$router.back()
            },
            handleNodeClick(data) {
                this.$refs.tree.setChecked(data.id, true)
            },
            handleResetChecked() {
                this.$refs.tree.setCheckedKeys(this.idKeys)
            },
            handleCheckedAll() {
                for(let item of this.data) {
                    this.$refs.tree.setChecked(item.id, true, true)
                }
            },
            handleRemoveCheckedAll() {
                this.$refs.tree.setCheckedKeys([])
            },
            getTreeData() {
                this.loading = true
                selectTree().then(res => {
                    this.data = res.data
                    this.getRolePermissionAssignList()
                    this.loading = false
                }).catch(error => {
                    this.loading = false
                    console.log(error)
                })
            },
            getRolePermissionAssignList() {
                getRolePermissionAssignList({id: this.$route.query.roleId}).then(response => {
                    for(let item of response.data) {
                        this.idKeys.push(item.permissionId)
                        this.$refs.tree.setChecked(item.permissionId, true, true)
                    }
                })
            },
            handleSavePermission() {
                this.idKeys = this.$refs.tree.getCheckedKeys(true)
                const data = {id: this.$route.query.roleId, permissionIds: this.idKeys}
                addPermissionToRole(data).then(response => {
                    if(response.data.status) {
                        this.$notify({
                            title: '成功', message: response.data.message,
                            type: 'success', duration: 2000
                        })
                        this.getTreeData()
                    } else {
                        this.$notify({
                            title: '操作失败', message: response.data.message,
                            type: 'error', duration: 2000
                        })
                    }
                }).catch(error => {
                })
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>